<template>
  <div class="login-container">
    <div class="login-box">
      <h2>用户登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-item">
          <div class="input-wrapper">
            <el-icon class="icon"><User /></el-icon>
            <input 
              v-model="account" 
              type="text" 
              placeholder="请输入账号"
              required
            />
          </div>
        </div>
        <div class="form-item">
          <div class="input-wrapper">
            <el-icon class="icon"><Lock /></el-icon>
            <input 
              v-model="password" 
              type="password" 
              placeholder="请输入密码"
              required
            />
          </div>
        </div>
        <button type="submit" class="login-btn">
          <el-icon class="button-icon"><Key /></el-icon>
          登录
        </button>
      </form>
      <div class="register-link">
        <a href="/register">注册账号</a>
      </div>
      <p v-if="message" :class="messageClass">{{ message }}</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { User, Lock, Key } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';
import { login } from '../../api/auth';
import { saveLoginInfo } from '../../utils/auth';

const router = useRouter();
const account = ref('');
const password = ref('');
const message = ref('');
const messageClass = ref('');

const handleLogin = async () => {
  try {
    const res = await login(account.value, password.value);
    
    if (res.success) {
      message.value = res.message || '登录成功';
      messageClass.value = 'success';
      
      // 保存token和用户信息
      // 如果后端返回了token，使用返回的token；否则使用临时token
      const token = res.data?.token || `user_token_${Date.now()}`;
      saveLoginInfo(
        token,
        res.data?.id,
        res.data?.account || account.value
      );
      
      // 跳转到首页
      setTimeout(() => {
        router.push('/');
      }, 500);
    } else {
      message.value = res.message || '登录失败，请检查账号和密码';
      messageClass.value = 'error';
    }
  } catch (error: any) {
    message.value = error.message || '登录失败,请检查账号和密码';
    messageClass.value = 'error';
  }
};
</script>

<style scoped>

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.login-container {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url("/images/bg.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  overflow: hidden;
  position: relative;
  margin: 0;
  padding: 0;
}

.login-box {
  background: rgba(255, 255, 255, 0.4);
  padding: 60px 40px;
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
  width: 100%;
  max-width: 400px;
  z-index: 10;
}

h2 {
  text-align: center;
  margin-bottom: 40px;
  color: #333;
  font-size: 1.8em;
}

.form-item {
  margin-bottom: 30px;
}

.input-wrapper {
  position: relative;
  width: 100%;
}

.icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
  font-size: 18px;
  z-index: 1;
}

.button-icon {
  margin-right: 8px;
  font-size: 18px;
}

input {
  width: 100%;
  padding: 16px 16px 16px 48px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  box-sizing: border-box;
  height: 50px;
}

input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.login-btn {
    width: 100%;
    padding: 16px;
    background: linear-gradient(135deg, #3a8a4a 0%, #408038 100%);
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 18px;
    cursor: pointer;
    transition: transform 0.2s, background 0.3s;
    height: 55px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

.login-btn:hover {
    transform: translateY(-2px);
    background: linear-gradient(135deg, #30753c 0%, #356e30 100%);
  }

p {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
}



.register-link {
  text-align: center;
  margin-top: 15px;
}

.register-link a {
  display: inline-block;
  color: #000000;
  font-size: 14px;
  font-weight: bold;
  text-decoration: none;
  padding: 5px 10px;
  transition: all 0.3s ease;
}

.register-link a:hover {
  color: #555555;
  text-decoration: underline;
}
</style>